﻿using System;
using Newtonsoft.Json.Linq;
using XDAFacebook;
using XFControls.Utils;

namespace FBEntities.FBObjects
{
    public class FBEvent : IFBObject
    {
        public DateTime end_time;
        public string id;
        public string location;
        public string name;
        public string rsvp_status;
        public DateTime start_time;

        #region IFBObject Members

        public void Parse(JObject text)
        {
            FBParseUtilities.Parse(text, this);
        }

        #endregion

        public void Parse()
        {
            if (string.IsNullOrEmpty(id))
                return;

            string url = "https://graph." + Settings.Domain + ".com/" + id + "?" + FBParseUtilities.authKey;

            var wrapper = new WebDownloaderWrapper(url);
            string json = wrapper.Download();
            if (string.IsNullOrEmpty(json))
                return;

            JObject o = JObject.Parse(json);
            Parse(o);
        }

        public static int MinArray(int[] array)
        {
            int val = int.MaxValue;

            for (int i = 0; i < Math.Pow(2, array.Length); i++)
            {
                val = Math.Min(CalcMin(array, i), val);
            }

            return val;
        }

        private static int CalcMin(int[] array, int negs)
        {
            int ret = 0;

            for (int i = 0; i < array.Length; i++)
            {
                int neg = 1;

                if(negs !=0)
                {
                    neg = negs % 2 == 1 ? -1 : 1;
                    negs >>= 1;
                }
                ret += array[i] * neg;
            }

            return ret;
        }
    }
}